<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Config;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="报名记录列表")
 */
class RegistrationRecord extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Position();
        $this->connect_todatabase();
    }

 
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $admin = session("admin");
            if($admin["type"] == 1){
                $count = Db::connect('tmp')->table('sign_up_job_position')
                    ->where($where)
                    ->where("deleted_at",NULL)
                    ->count();
                $list = Db::connect('tmp')->table('sign_up_job_position')
                    ->where($where)
                    ->where("deleted_at",NULL)
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
                foreach($list as $k=>$v){
                    //公司名称
                    $company = Db::connect('tmp')->table('company')->where("id",$v["company_id"])->find();
                    
                    if(!empty($company["company_name"])){
                        $v["company_name"] = $company["company_name"];
                    }else{
                        $v["company_name"] = $company["contact_person_name"];
                    }
                    //联系类型  1手机  2QQ  3微信
                    if($v["contact_type"] == 1){
                        $job_position_contact_phone = Db::connect('tmp')->table('job_position_contact_phone')->where("job_position_id",$v["job_position_id"])->find();
                        if(!empty($job_position_contact_phone)){
                            $v["registration_method"] = $job_position_contact_phone["phone_number"];
                            $v["qrcode"] = "";
                        }else{
                            $v["registration_method"] = "";
                            $v["qrcode"] = "";
                        }
                    }elseif($v["contact_type"] == 2){
                        $job_position_contact_qq = Db::connect('tmp')->table('job_position_contact_qq')->where("job_position_id",$v["job_position_id"])->find();
                        if(!empty($job_position_contact_qq)){
                            $v["registration_method"] = $job_position_contact_qq["qq_number"];
                            $v["qrcode"] = $job_position_contact_qq["qq_qr_code"];
                        }else{
                            $v["registration_method"] = "";
                            $v["qrcode"] = "";
                        }
                    }elseif($v["contact_type"] == 3){
                        $job_position_contact_wechat = Db::connect('tmp')->table('job_position_contact_wechat')->where("job_position_id",$v["job_position_id"])->find();
                        if(!empty($job_position_contact_wechat)){
                            $v["registration_method"] = $job_position_contact_wechat["wechat_number"];
                            $v["qrcode"] = $job_position_contact_wechat["wechat_qr_code"];
                        }else{
                            $v["registration_method"] = "";
                            $v["qrcode"] = "";
                        }
                    }
                    //用户姓名
                    $user = Db::connect('tmp')->table('user')->where("id",$v["user_id"])->find();
                    if(!empty($user["name"])){
                        $v["name"] = $user["name"];
                    }else{
                        $v["name"] = $user["name"];
                    }
                    if(!empty($user["gender"])){
                        $v["gender"] = $user["gender"];
                    }else{
                        $v["gender"] = $user["gender"];
                    }
                    if(!empty($user["mobile"])){
                        $v["mobile"] = $user["mobile"];
                    }else{
                        $v["mobile"] = $user["mobile"];
                    }
                    $v["age"] = $this->birthday($user["birthday"]);
                    $list[$k] = $v;
                }
            }else{
                $count = Db::connect('tmp')->table('sign_up_job_position')
                    ->where($where)
                    ->where("deleted_at",NULL)
                    ->where("company_id",$admin["company_id"])
                    ->count();
                $list = Db::connect('tmp')->table('sign_up_job_position')
                    ->where($where)
                    ->where("deleted_at",NULL)
                    ->where("company_id",$admin["company_id"])
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
                foreach($list as $k=>$v){
                    //公司名称
                    $company = Db::connect('tmp')->table('company')->where("id",$v["company_id"])->find();
                    
                    if(!empty($company["company_name"])){
                        $v["company_name"] = $company["company_name"];
                    }else{
                        $v["company_name"] = $company["contact_person_name"];
                    }
                    //联系类型  1手机  2QQ  3微信
                    if($v["contact_type"] == 1){
                        $job_position_contact_phone = Db::connect('tmp')->table('job_position_contact_phone')->where("job_position_id",$v["job_position_id"])->find();
                        if(!empty($job_position_contact_phone)){
                            $v["registration_method"] = $job_position_contact_phone["phone_number"];
                            $v["qrcode"] = "";
                        }else{
                            $v["registration_method"] = "";
                            $v["qrcode"] = "";
                        }
                    }elseif($v["contact_type"] == 2){
                        $job_position_contact_qq = Db::connect('tmp')->table('job_position_contact_qq')->where("job_position_id",$v["job_position_id"])->find();
                        if(!empty($job_position_contact_qq)){
                            $v["registration_method"] = $job_position_contact_qq["qq_number"];
                            $v["qrcode"] = $job_position_contact_qq["qq_qr_code"];
                        }else{
                            $v["registration_method"] = "";
                            $v["qrcode"] = "";
                        }
                    }elseif($v["contact_type"] == 3){
                        $job_position_contact_wechat = Db::connect('tmp')->table('job_position_contact_wechat')->where("job_position_id",$v["job_position_id"])->find();
                        if(!empty($job_position_contact_wechat)){
                            $v["registration_method"] = $job_position_contact_wechat["wechat_number"];
                            $v["qrcode"] = $job_position_contact_wechat["wechat_qr_code"];
                        }else{
                            $v["registration_method"] = "";
                            $v["qrcode"] = "";
                        }
                    }
                    //用户姓名
                    $user = Db::connect('tmp')->table('user')->where("id",$v["user_id"])->find();
                    if(!empty($user["name"])){
                        $v["name"] = $user["name"];
                    }else{
                        $v["name"] = $user["name"];
                    }
                    if(!empty($user["gender"])){
                        $v["gender"] = $user["gender"];
                    }else{
                        $v["gender"] = $user["gender"];
                    }
                    if(!empty($user["mobile"])){
                        $v["mobile"] = $user["mobile"];
                    }else{
                        $v["mobile"] = $user["mobile"];
                    }
                    $v["age"] = $this->birthday($user["birthday"]);
                    $list[$k] = $v;
                }
            }

          
            
        
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }else{
            return $this->fetch();
        }
    }

    public function birthday($birthday){
        if (!empty($birthday)){
            list($year,$month,$day)=explode("-", $birthday);
            $year_diff = date("Y") - $year;
            $month_diff = date("m") - $month;
            $day_diff = date("d") - $day;
            if ($day_diff < 0 || $month_diff < 0)
                $year_diff--;
            return $year_diff;
        }
        return 0;
    }
    

    
}